package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.admin.service.QqService;
import cn.hinglo.gameclaw.admin.service.VersionService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.data.utils.CommonUtils;
import cn.hinglo.gameclaw.data.utils.FieldUtils;
import cn.hinglo.gameclaw.user.bean.Version;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @createTime: 2018/5/10
 * @author: HingLo
 * @description: 通用信息查询
 */
@RestController
@RequestMapping("/client/com/")
public class CommonClientAction extends BaseController {
    /**
     * QQ群信息
     */
    private final QqService qqService;

    private final VersionService versionService;

    @Autowired
    public CommonClientAction(QqService qqService, VersionService versionService) {
        this.qqService = qqService;
        this.versionService = versionService;
    }

    /**
     * 客户端QQ群查看
     *
     * @return qq群信息
     */
    @GetMapping("qq")
    public Result queryQQAction() {
        return success(qqService.queryQQService());
    }

    /**
     * 客户端检测版本更新状态<br>
     * 客服端使用了别人的框架，所以返回值违反了规范。直接返回了Map
     *
     * @return 返回最新版本的信息
     */
    @GetMapping("check")
    public Map<String, Object> checkVersion(@RequestParam("v") String v) {
        List<Version> list = versionService.listService();
        Version version = list.get(list.size() - 1);
        Map<String, Object> map = new HashMap<>(7);
        Boolean bool = version.getNum().equalsIgnoreCase(v);
        map.put("update", bool ? "no" : "Yes");
        map.put("apk_file_url", CommonUtils.imgInstall(version.getName(), FieldUtils.IMAGE_8));
        map.put("new_version", version.getNum());
        map.put("update_log", version.getContent());
        map.put("target_size", version.getSize() + "M");
        map.put("new_md5", version.getMd5());
        //是否强制下载
        map.put("constraint", false);
        //组装下载链接
        return map;
    }

}
